<template>
  <div id="subpage">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <b>权限设置</b>
        </el-breadcrumb-item>
        <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
    <div class="content">
      <div class="xcx-head">
        <div style="display: flex">
          <span>管理员列表</span>
        </div>
        <el-button @click="dialogOrderVisible = true" size="small" icon="el-icon-plus" type="success">添加管理员</el-button>
      </div>
      <div class="xcx-content">
        <!--列表-->
        <el-table border size="mini" :data="tableData" stripe style="width: 100%" v-loading="loading">
          <el-table-column align="center" prop="id" label="UID" width="80"></el-table-column>
          <el-table-column align="center" label="用户名" prop="username" width="160"></el-table-column>
          <el-table-column align="center" label="真实姓名" prop="truename" width="160"></el-table-column>
          <el-table-column align="center" label="手机号" prop="mobile" width="160"></el-table-column>
          <el-table-column label="" width="*"></el-table-column>
          <el-table-column align="center" prop="role_name" label="角色名称" width="180"></el-table-column>
          <el-table-column align="center" label="状态" width="100">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                @change="setStatus(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="created_at" width="160" label="添加时间"></el-table-column>
          <el-table-column align="center" label="操作" width="180">
            <template #default="scope">
              <el-button size="mini" icon="el-icon-edit" type="primary" plain @click="edit(scope.row.id)">编辑</el-button>
              <el-button size="mini" icon="el-icon-delete" type="danger" plain
                @click="del_item(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="paging">
          <el-pagination class="left" @current-change="handleCurrentChange" :current-page="page" background
            layout="prev, pager, next" :total="count"></el-pagination>
        </div>
      </div>
    </div>

    <el-dialog title="添加管理员" :visible.sync="dialogOrderVisible" :close-on-click-modal="false" width="35%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="角色类型：" prop="role_id" label-width="100px">
          <el-select v-model="ruleForm.role_id" placeholder="请选择">
            <el-option v-for="(item, indexs) in roleData" :key="indexs" :label="item.role_name"
              :value="item.role_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名：" prop="username" label-width="100px">
          <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名：" prop="truename" label-width="100px">
          <el-input v-model="ruleForm.truename" placeholder="请输入真实姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="mobile" label-width="100px">
          <el-input v-model="ruleForm.mobile" autocomplete="off" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password" label-width="100px">
          <el-input type="password" autocomplete="off" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogOrderVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmDesc('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogOrderVisible: false,
      loading: true,
      tableData: [],
      page: 0,
      count: 0,
      limit: 10,
      ruleForm: {
        role_id: "",
        class_id: [],
        username: "",
        truename: "",
        mobile: "",
        password: "",
      },
      rules: {
        role_id: [{ required: true, message: "请选择角色", trigger: "change" }],
        username: [
          { required: true, message: "请填写用户名", trigger: "blur" },
        ],
        mobile: [{ required: true, message: "请填写手机号", trigger: "blur" }],
        password: [{ required: true, message: "请填写密码", trigger: "blur" }],
      },
      roleData: [],
    };
  },
  //进入页面加载
  mounted: function () {
    var that = this;
    that.getList();
    that.getRoleList();
  },
  methods: {

    getRoleList() {
      var that = this;
      that.axios.post("/gerRoleList").then((res) => {
        if (res.code == 200) {
          that.roleData = res.data;
        }
        that.loading = false;
      });
    },

    confirmDesc(formName) {
      const that = this;
      console.log(formName);
      that.$refs[formName].validate((valid) => {
        if (!valid) return that.$message.warning("请完整填写内容!");
        var formData = {};
        formData = that.ruleForm;
        if (that.id) {
          formData.id = that.id;
        }
        that.axios.post("/admin/beforeSave", formData).then(() => {
          that.$message.success(that.id ? "修改成功" : "添加成功");
          that.ruleForm.username = "";
          that.ruleForm.truename = "";
          that.ruleForm.mobile = "";
          that.ruleForm.password = "";
          that.ruleForm.id = 0;
          that.dialogOrderVisible = false;
          that.getList();
        });
      });
    },

    edit(ids) {
      var that = this;
      this.ruleForm.id = ids;
      this.axios.post("/admin/detail", { id: ids }).then((res) => {
        if (res.code == 200) {
          that.ruleForm = res.data;
        }
        that.loading = false;
      });
      this.dialogOrderVisible = true;
    },

    setStatus(row) {
      var that = this;
      that.axios.post("/admin/setStatus", {
        id: row.id,
        status: row.status,
      });
    },

    /**
     * 下一页
     */
    handleCurrentChange(currentPage) {
      var that = this;
      that.page = currentPage;
      that.getList();
    },

    del_item(ids) {
      let that = this;
      this.$confirm("此操作不可逆, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        that.axios
          .post("/admin/destroy", {
            ids: ids,
          })
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              that.getList();
            } else {
              this.$message({
                type: "info",
                message: res.msg,
              });
            }
          });
      });
    },

    /**
     * 获取列表
     */
    getList() {
      let that = this;
      let formData = {};
      var curr_page = sessionStorage.getItem("curr_page");
      that.page = curr_page ? curr_page - 0 : that.page;
      formData.page = that.page;
      formData.limit = that.limit;
      that.axios.post("/admin/index", formData).then((res) => {
        if (res) {
          that.tableData = res.data.data;
          that.count = res.data.total;
          that.page = curr_page ? curr_page : that.page;
          curr_page ? sessionStorage.removeItem("curr_page") : "";
          that.loading = false;
        }
      });
    },
  },
};
</script>
<style>
.seeting {
  padding: 10px 0;
  line-height: 24px;
}
</style>